package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.McxsbgBsDetail;
import com.ruoyi.report_ljlm.domain.vo.McxsbgBsDetailVo;
import com.ruoyi.report_ljlm.mapper.McxsbgBsDetailMapper;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.McxsbgBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.McxsbgBsInfoVo;
import com.ruoyi.report_ljlm.domain.McxsbgBsInfo;
import com.ruoyi.report_ljlm.mapper.McxsbgBsInfoMapper;
import com.ruoyi.report_ljlm.service.IMcxsbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路面摩擦系数试验检测报告Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class McxsbgBsInfoServiceImpl implements IMcxsbgBsInfoService {

    private final McxsbgBsInfoMapper baseMapper;
    private final McxsbgBsDetailMapper detailMapper;

    /**
     * 查询路面摩擦系数试验检测报告
     */
    @Override
    @FIllBasicInfo
    public McxsbgBsInfoVo queryById(Long excelId) {
        McxsbgBsInfoVo mcxsbgBsInfoVo = baseMapper.selectVoById(excelId);
        List<McxsbgBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<McxsbgBsDetail>().eq(McxsbgBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            mcxsbgBsInfoVo.setDetailList(detailList);
        }
        return mcxsbgBsInfoVo;
    }

    /**
     * 查询路面摩擦系数试验检测报告列表
     */
    @Override
    public TableDataInfo<McxsbgBsInfoVo> queryPageList(McxsbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<McxsbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<McxsbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路面摩擦系数试验检测报告列表
     */
    @Override
    public List<McxsbgBsInfoVo> queryList(McxsbgBsInfoBo bo) {
        LambdaQueryWrapper<McxsbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<McxsbgBsInfo> buildQueryWrapper(McxsbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<McxsbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, McxsbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, McxsbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), McxsbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), McxsbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), McxsbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), McxsbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), McxsbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), McxsbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), McxsbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), McxsbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), McxsbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), McxsbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), McxsbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), McxsbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), McxsbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), McxsbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), McxsbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), McxsbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), McxsbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), McxsbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), McxsbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), McxsbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), McxsbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), McxsbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), McxsbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增路面摩擦系数试验检测报告
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(McxsbgBsInfoBo bo) {
        McxsbgBsInfo add = BeanUtil.toBean(bo, McxsbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<McxsbgBsDetail> details = new ArrayList<>(12);
            for (int i = 1; i <= 12; i++) {
                details.add(new McxsbgBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改路面摩擦系数试验检测报告
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(McxsbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        McxsbgBsInfo update = BeanUtil.toBean(bo, McxsbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), McxsbgBsDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(McxsbgBsInfoBo bo) {
        if ("不符合".equals(bo.getJiegpd())) {
            bo.setExcelResult(2);
            return;
        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(McxsbgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路面摩擦系数试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public McxsbgBsInfoVo jcjl(McxsbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
